/*=====================
 * name    :  rock
 * author  :  zjsina
 * version :  0.1.0
 * date    :  2014-6-26
 ======================*/
(function( window, undefined ) {
	//support: android 2.2+ IOS 4.0+ windows phone 8.0+
	var
	//version
	r_version = "0.1.0",
	
	//previous Alpha
	pAlpha, 
	
	//now Alpha
	nAlpha, 
	
	//previous Time
	pTime, 
	
	//now Time
	nTime, 
	
	//minimum trigger conditions 
	minrock,
	
	//Array for rock speed
	speedHandler = [], 
	
	//avoid hand shaking
	rockOffset = 5, 
	
	//decimal place
	digital = 4,  
	
	//rockObj
	rockObj, 
	
	//callback function
	rockCallbackFn;

	//rock初始化
	rock = function( minrange ) {
		return new rock.fn.init( minrange );
	}
	rock.fn = rock.prototype = {
		// The current version of rock being used
		rock : r_version,

		//constructor
		constructor : rock,

		//rock init
		init : function( minrange ) {
			//最小摇动范围
			if ( minrock == undefined ) 
				minrock = minrange;
			return this;
		},
		
		//rock开始
		start : function( callback ) {
			//rockObj 初始化
			rockObj = {
				state     :   0,   //状态值: 1.移动中;0.设备静止;-1.位移未满足最小计量
				Alpha     :   0,   //位移值  
				speed     :   0,   //位移速度
				times     :   0,   //满足条件次数
				maxSpeed  :   0,   //最大速度
				aveSpeed  :   0    //平均速度
			};
			//添加事件 
			rock.addEvent( window, 'deviceorientation', this.orientationHandler );
			rockCallbackFn = callback;
		},
		
		//rock 停止
		stop : function() {
			//取消事件
			rock.removeEvent( window, 'deviceorientation', this.orientationHandler );
		},
		
		//rock 事件处理
		orientationHandler : function( e ) {
			var 
			//现速度
			nspeed, 
			
			//速度之和(平均速度)
			speedc = 0, 
			
			//speedHandler的length(提前声明，避免多次查询)
			speedHandlerL;
			
			//参数初始化
			if ( pAlpha == undefined ) {
				pAlpha =  parseInt( e.alpha );
				pTime  =  new Date().getTime() - 1;
			}
			nAlpha = rockObj.Alpha = parseInt( e.alpha );
			
			//状态 1: 满足最小计量移动
			if (Math.abs( nAlpha - pAlpha ) > minrock && Math.abs( nAlpha - pAlpha ) < 360 - minrock) {
				//获取时间
				nTime = new Date().getTime();
				//近似速度表示，以时间差表示速度
				nspeed = (1 / ( nTime - pTime )).toFixed( digital );
				//最大速度判断
				if (nspeed > rockObj.maxSpeed)
					rockObj.maxSpeed = nspeed;
				rockObj.times++;
				rockObj.state = 1;
				rockObj.speed = nspeed;
				speedHandler.push( nspeed );
				//平均速度判断
				speedHandlerL = speedHandler.length;
				for ( var i = 0; i < speedHandlerL; i++ )
					if ( !isNaN( speedHandler[i] ) )
						speedc += Number( speedHandler[i] )
				rockObj.aveSpeed = ( speedc / speedHandlerL ).toFixed( digital );
			} 
			//状态 -1: 不满足满足最小计量移动
			else if ( Math.abs( nAlpha - pAlpha ) > rockOffset ) {
				rockObj.state = -1;
			} 
			//状态 0: 近似静止
			else {
				rockObj.state = 0;
				rockObj.speed = 0;
			}
			rockCallbackFn( rockObj );
			pAlpha =  nAlpha;
			pTime  =  nTime;
		}
	}
	rock.fn.init.prototype = rock.fn;
	//PC 判断
	rock.isPc = function() {
		var 
		//userAgent
		userAgentInfo = navigator.userAgent.toLowerCase(), 
		
		//移动设备userAgent (ps:windows phone 关键词 改为 touch)
		Agents = new Array("android", "iphone", "symbianos", "touch", "ipad", "ipod"), 
		
		//Agents Length
		AgentsL = 6, 
		
		//return value
		flag = true;
		
		//字符串检验
		for ( var v = 0; v < Agents.length; v++ )
			if ( userAgentInfo.indexOf( Agents[v] ) > 0 ) {
				flag = false;
				break;
			}
		return flag;
	}
	//检测是否支持事件获取
	rock.test = function(){
		if ( !window.DeviceOrientationEvent ) 
			return false;
		else 
			return true;
	}
	//事件绑定
	rock.addEvent = function( el, type, fn ){
		/* android Iphone */
		if ( el.addEventListener ) 
			el.addEventListener( type, fn, false );
		/* windows phone */
		else if ( el.attachEvent ) 
			el.attachEvent('on' + type, fn);
	}
	//绑定解除
	rock.removeEvent = function( el, type, fn ){
		/* android Iphone */
		if ( el.removeEventListener ) 
			el.removeEventListener( type, fn, false );
		/* windows phone */
		else if ( el.detchEvent ) 
			el.detchEvent('on' + type, fn);
	}
	//重置
	rock.restart = function( callback ){
		//speedHandler 重置
		speedHandler = [];
		//事件再次注册
		rock.fn.start( callback );
	}
	// dom ready
	rock.DomReady = function( callback ) {
		/* android Iphone */
		if ( document.addEventListener )
			document.addEventListener("DOMContentLoaded", callback(), false);
		
		/* windows phone */
		else if ( document.attachEvent )
			document.attachEvent("onreadystatechange", function() {
				if ( document.readyState === "complete" )
					callback();
			});
	}
	//添加rock对象
	window.rock = rock;
})(window)